﻿/*
    File name:          TechLogViewModel
    File type:          Class
    Author:             Mads Skipper Pedersen 07299
    Creation date:      12-04-2011
    Last modified:      18-05-2011
    Last modified by:   Mads Skipper Pedersen 07299
    Description:        The ViewModel for the Log viewer
                        Here logs from the application can be seen
                        This includes error messages from the robot.
                        Only visible to users with at least technician rights.
*/

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using i4pro_gruppe_d.Database;
using i4pro_gruppe_d.Types;

namespace TechLog.ViewModel 
{
    public class TechLogViewModel : ViewModelBase
    {
        #region Members
        private BackgroundWorker bgw;
        private AbstractDatabase db;
        private int selectedSort;
        #endregion
        #region Propertys
        public ObservableCollection<Log> LogsAll { get; set; }
        public ObservableCollection<Log> LogsFiltered { get; set; }
        private DateTime selectedDate; 
        public DateTime SelectedDate 
        { 
            get { return selectedDate; }
            set 
            { 
                selectedDate = value;
                RaisePropertyChanged("SelectedDate");
            }
        }
        private DateTime startDate;
        public DateTime StartDate
        {
            get { return startDate; }
            set
            {
                startDate = value;
                RaisePropertyChanged("StartDate");
            }
        }
        private DateTime endDate;
        public DateTime EndDate
        {
            get { return endDate; }
            set
            {
                endDate = value;
                RaisePropertyChanged("EndDate");
            }
        }

        private FontWeight fontAll;
        public FontWeight FontAll 
        { 
            get { return fontAll; }
            set 
            { 
                fontAll = value;
                RaisePropertyChanged("FontAll");
            }
        }
        private FontWeight fontNormal;
        public FontWeight FontNormal
        {
            get { return fontNormal; }
            set
            {
                fontNormal = value;
                RaisePropertyChanged("FontNormal");
            }
        }
        
        private FontWeight fontCommand;
        public FontWeight FontCommand
        {
            get { return fontCommand; }
            set
            {
                fontCommand = value;
                RaisePropertyChanged("FontCommand");
            }
        }
        private FontWeight fontError;
        public FontWeight FontError
        {
            get { return fontError; }
            set
            {
                fontError = value;
                RaisePropertyChanged("FontError");
            }
        }
        private FontWeight fontEmergency;
        public FontWeight FontEmergency
        {
            get { return fontEmergency; }
            set
            {
                fontEmergency = value;
                RaisePropertyChanged("FontEmergency");
            }
        }

        #endregion
        #region Constructor
        public TechLogViewModel()
        {
            AbstractDatabase _db = AbstractDatabase.Instance;
            db = _db;
            
            LogsAll = new ObservableCollection<Log>();
            LogsFiltered = new ObservableCollection<Log>();
            selectedSort = 0;

            StartDate = new DateTime(2011, 1, 1);
            EndDate = new DateTime(2011, 7, 31);
            SelectedDate = DateTime.Now;

            fontAll = FontWeights.Normal;
            fontNormal = FontWeights.Normal;
            fontCommand = FontWeights.Normal;
            fontError = FontWeights.Normal;
            fontEmergency = FontWeights.Normal;

            DateChangedExecute();
        }
        #endregion
        #region Commands
        #region Previous History Command
        public ICommand PrevHistory_cmd
        {
            get { return new RelayCommand(PrevHistoryExecute, PrevHistoryCanExecute); }
        }

        private void PrevHistoryExecute()
        {
            SelectedDate = SelectedDate - new TimeSpan(1, 0, 0, 0);
        }

        private bool PrevHistoryCanExecute()
        {
            if (StartDate < SelectedDate)
                return true;
            return false;
        }
        #endregion
        #region Next History Command
        public ICommand NextHistory_cmd
        {
            get { return new RelayCommand(NextHistoryExecute, NextHistoryCanExecute); }
        }

        private void NextHistoryExecute()
        {
            SelectedDate = SelectedDate + new TimeSpan(1, 0, 0, 0);
        }

        private bool NextHistoryCanExecute()
        {
            if (EndDate > SelectedDate)
                return true;
            return false;
        }
        #endregion
        #region Date Selection Changed Command
        public ICommand DateSelectionChanged_Command
        {
            get { return new RelayCommand(DateChangedExecute); }
        }

        private void DateChangedExecute()
        {
            if (SelectedDate < StartDate)
            {
                SelectedDate = StartDate;
            }
            else if (SelectedDate > EndDate)
            {
                SelectedDate = EndDate;
            }
            Run_BGW();
        }
        #endregion
        #region Filter All Command
        public ICommand FilterAll_cmd
        {
            get { return new RelayCommand(FilterAllExecute); }
        }

        private void FilterAllExecute()
        {
            selectedSort = 0;
            FilterLogs();
            HighlightFilter();
        }
        #endregion
        #region Filter Normal Command
        public ICommand FilterNormal_cmd
        {
            get { return new RelayCommand(FilterNormalExecute); }
        }

        private void FilterNormalExecute()
        {
            selectedSort = 1;
            FilterLogs();
            HighlightFilter();
        }
        #endregion
        #region Filter Command Command
        public ICommand FilterCommand_cmd
        {
            get { return new RelayCommand(FilterCommandExecute); }
        }

        private void FilterCommandExecute()
        {
            selectedSort = 2;
            FilterLogs();
            HighlightFilter();
        }
        #endregion
        #region Filter Error Command
        public ICommand FilterError_cmd
        {
            get { return new RelayCommand(FilterErrorExecute); }
        }

        private void FilterErrorExecute()
        {
            selectedSort = 3;
            FilterLogs();
            HighlightFilter();
        }
        #endregion
        #region Filter Emergency Command
        public ICommand FilterEmergency_cmd
        {
            get { return new RelayCommand(FilterEmergencyExecute); }
        }

        private void FilterEmergencyExecute()
        {
            selectedSort = 4;
            FilterLogs();
            HighlightFilter();
        }
        #endregion
        #region Refresh Logs List Command
        public ICommand RefreshLogsList_Cmd
        {
            get { return new RelayCommand(RefreshLogsList); }
        }

        public void RefreshLogsList()
        {
            DateChangedExecute();
        }
        #endregion
        #endregion
        #region Methods
        private void FilterLogs()
        {
            LogsFiltered.Clear();
            if (selectedSort != 0)
            {
                var found1 = from o in LogsAll
                             where o.LogType == selectedSort
                             select o;

                try
                {
                    foreach (var log in found1)
                    {
                        LogsFiltered.Add(log);
                    }
                }
                catch (Exception)
                {
                    
                }
                
                
            }
            else
            {
                try
                {
                    foreach (Log log in LogsAll)
                    {
                        LogsFiltered.Add(log);
                    }
                }
                
                catch(Exception)
                {
                }
            }
            
        }

        private void HighlightFilter()
        {
            FontNormal = FontWeights.Normal;
            FontCommand = FontWeights.Normal;
            FontError = FontWeights.Normal;
            FontEmergency = FontWeights.Normal;

            switch (selectedSort)
            {
                case 1:
                    FontNormal = FontWeights.Bold;
                    break;
                case 2:
                    FontCommand = FontWeights.Bold;
                    break;
                case 3:
                    FontError = FontWeights.Bold;
                    break;
                case 4:
                    FontEmergency = FontWeights.Bold;
                    break;
            }
        }
        #endregion
        #region Background Worker
        private void Run_BGW()
        {
            bgw = new BackgroundWorker();
            bgw.DoWork += Bgw_GetDB_DoWork;
            bgw.RunWorkerCompleted += Bgw_GetDB_Complete;
            bgw.RunWorkerAsync();
        }
        private void Bgw_GetDB_DoWork(Object sender, DoWorkEventArgs e)
        {
            List<Log> tempList = new List<Log>();
            tempList = db.ReadLog(SelectedDate);
            LogsAll.Clear();

            foreach (var log in tempList)
            {
                LogsAll.Add(log);
            }
        }

        private void Bgw_GetDB_Complete(Object sender, RunWorkerCompletedEventArgs e)
        {
            FilterLogs();
        }
        #endregion
    }
}
